package coupon_distribution

import (
	// "bp/cache"
	"bp/conf"
	"bp/db"

	// "bgs/grpc/client"
	internalHttpMiddleware "bp/internal/middleware/http"
	"bp/internal/service/entity"
	"bp/internal/twirpex"
	"go.uber.org/zap"

	// "bp/internal/util"
	messageGrpc "bp/rpc/gen/shared/message"
	couponDistributionGrpc "bp/rpc/gen/webapi/coupon_distribution"
	"context"

	"github.com/go-chi/chi"
	"github.com/golang/protobuf/ptypes/empty"
)

type CouponDistributionWebApiImp struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	vars      *conf.VarsSection
	entityHub *entity.EntityHub
	// cacher *cache.Adapter
	couponDistributionGrpc.CouponDistributionOpService
}

// NewCouponDistributionWebApi initialization
func NewCouponDistributionWebApi(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	entityHub *entity.EntityHub,
	// adp *cache.Adapter,
) (ws *CouponDistributionWebApiImp) {
	ws = &CouponDistributionWebApiImp{
		dao:       pgdao,
		slog:      slog.With("module", "CouponDistributionWebApi"),
		vars:      vars,
		entityHub: entityHub,
		// cacher:           adp,
	}
	return
}

const (
	EntityName                            string = "优惠券投放"
	PingCouponDistributionMethod          string = "/coupon_distribution/ping/exec"
	CreateCouponDistributionMethod        string = "/coupon_distribution"              // POST
	UpdateCouponDistributionMethod        string = "/coupon_distribution"              // PUT
	ExecuteCouponDistributionMethod       string = "/coupon_distribution/{id}/execute" // PUT
	GetCouponDistributionMethod           string = "/coupon_distribution/{id}"         // GET
	ListCouponDistributionsMethod         string = "/coupon_distributions"             // GET
	ListCouponDistributionWithStatsMethod string = "/coupon_distributionWithStats"     // GET
)

// RegisterToWeb 将部分grpc方法注册到web
func (s *CouponDistributionWebApiImp) RegisterToWeb(r chi.Router) {
	couponDistributionRT := twirpex.NewSingleTwirpServerProxy(couponDistributionGrpc.NewCouponDistributionOpServiceServer(s))

	r.Group(func(r chi.Router) {
		r.Get(PingCouponDistributionMethod, couponDistributionRT.Handle("PingCouponDistribution"))
		r.Group(func(r chi.Router) {
			r.Use(internalHttpMiddleware.WithBilinToken(s.slog, s.vars))
			r.Post(CreateCouponDistributionMethod, couponDistributionRT.Handle("CreateCouponDistribution"))
			r.Put(UpdateCouponDistributionMethod, couponDistributionRT.Handle("UpdateCouponDistribution"))
			r.Put(ExecuteCouponDistributionMethod, couponDistributionRT.Handle("ExecuteCouponDistribution"))
			r.Get(GetCouponDistributionMethod, couponDistributionRT.Handle("GetCouponDistribution"))
			r.Group(func(r chi.Router) {
				r.Use(internalHttpMiddleware.WithPager(s.slog, s.vars))
				r.Get(ListCouponDistributionsMethod, couponDistributionRT.Handle("ListCouponDistributions"))
				r.Get(ListCouponDistributionWithStatsMethod, couponDistributionRT.Handle("ListCouponDistributionWithStats"))
			})
		})
	})
}

func (s *CouponDistributionWebApiImp) PingCouponDistribution(ctx context.Context, _ *empty.Empty) (res *messageGrpc.PingRes, err error) {
	res = &messageGrpc.PingRes{
		Msg: "ok",
	}
	return
}
